package com.ruoyi.miniapp.controller;


import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.miniapp.api.domain.MiniappDept;
import com.ruoyi.miniapp.api.domain.MiniappRole;
import com.ruoyi.miniapp.service.IMiniappDeptService;
import com.ruoyi.miniapp.service.IMiniappRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 角色信息Controller
 *
 * @author ruoyi
 * @date 2023-09-17
 */
@RestController
@RequestMapping("/role")
public class MiniappRoleController extends BaseController
{
    @Autowired
    private IMiniappRoleService miniappRoleService;
    @Autowired
    private IMiniappDeptService miniappDeptService;
    /**
     * 查询角色信息列表
     */
    @RequiresPermissions("miniapp:role:list")
    @GetMapping("/list")
    public TableDataInfo list(MiniappRole miniappRole)
    {
        startPage();
        List<MiniappRole> list = miniappRoleService.selectMiniappRoleList(miniappRole);
        return getDataTable(list);
    }

    /**
     * 导出角色信息列表
     */
    @RequiresPermissions("miniapp:role:export")
    @Log(title = "角色信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MiniappRole miniappRole)
    {
        List<MiniappRole> list = miniappRoleService.selectMiniappRoleList(miniappRole);
        ExcelUtil<MiniappRole> util = new ExcelUtil<MiniappRole>(MiniappRole.class);
        util.exportExcel(response, list, "角色信息数据");
    }

    /**
     * 获取角色信息详细信息
     */
    @RequiresPermissions("miniapp:role:query")
    @GetMapping(value = "/{roleId}")
    public AjaxResult getInfo(@PathVariable("roleId") Long roleId)
    {
        return success(miniappRoleService.selectMiniappRoleByRoleId(roleId));
    }

    /**
     * 新增角色信息
     */
    @RequiresPermissions("miniapp:role:add")
    @Log(title = "角色信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody MiniappRole miniappRole)
    {
        if (!miniappRoleService.checkRoleNameUnique(miniappRole))
        {
            return error("新增角色'" + miniappRole.getRoleName() + "'失败，角色名称已存在");
        }
        else if (!miniappRoleService.checkRoleKeyUnique(miniappRole))
        {
            return error("新增角色'" + miniappRole.getRoleName() + "'失败，角色权限已存在");
        }
        miniappRole.setCreateBy(SecurityUtils.getUsername());
        return toAjax(miniappRoleService.insertMiniappRole(miniappRole));
    }

    /**
     * 修改角色信息
     */
    @RequiresPermissions("miniapp:role:edit")
    @Log(title = "角色信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MiniappRole miniappRole)
    {
//        miniappRoleService.checkRoleAllowed(miniappRole);
        miniappRoleService.checkRoleDataScope(miniappRole.getRoleId());
        if (!miniappRoleService.checkRoleNameUnique(miniappRole))
        {
            return error("修改角色'" + miniappRole.getRoleName() + "'失败，角色名称已存在");
        }
        else if (!miniappRoleService.checkRoleKeyUnique(miniappRole))
        {
            return error("修改角色'" + miniappRole.getRoleName() + "'失败，角色权限已存在");
        }
        miniappRole.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(miniappRoleService.updateMiniappRole(miniappRole));
    }

    /**
     * 删除角色信息
     */
    @RequiresPermissions("miniapp:role:remove")
    @Log(title = "角色信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{roleIds}")
    public AjaxResult remove(@PathVariable Long[] roleIds)
    {
        return toAjax(miniappRoleService.deleteMiniappRoleByRoleIds(roleIds));
    }

    /**
     * 状态修改
     */
    @RequiresPermissions("system:role:edit")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody MiniappRole miniappRole)
    {
        miniappRoleService.checkRoleAllowed(miniappRole);
        miniappRoleService.checkRoleDataScope(miniappRole.getRoleId());
        miniappRole.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(miniappRoleService.updateRoleStatus(miniappRole));
    }

    /**
     * 获取对应角色部门树列表
     */
    @RequiresPermissions("miniapp:role:query")
    @GetMapping(value = "/deptTree/{roleId}")
    public AjaxResult deptTree(@PathVariable("roleId") Long roleId)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", miniappDeptService.selectDeptListByRoleId(roleId));
        ajax.put("depts", miniappDeptService.selectDeptTreeList(new MiniappDept()));
        return ajax;
    }


    /**
     * 修改保存数据权限
     */
    @RequiresPermissions("miniapp:role:edit")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping("/dataScope")
    public AjaxResult dataScope(@RequestBody MiniappRole miniappRole)
    {
        miniappRoleService.checkRoleAllowed(miniappRole);
        miniappRoleService.checkRoleDataScope(miniappRole.getRoleId());
        return toAjax(miniappRoleService.authDataScope(miniappRole));
    }
}
